package com.iecampus.fragment;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.iecampus.activity.GoodsDetailActivity;
import com.iecampus.activity.R;
import com.iecampus.adapter.GoodsListAdapter;
import com.iecampus.adapter.UserDetailAdapter;
import com.iecampus.moldel.Goods;
import com.iecampus.utils.JsonUtil;
import com.iecampus.view.MyProgressDialog;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class PersonalInfoFragment extends Fragment implements
		OnItemClickListener {
	private ListView listView;
	private Type typeToken;
	private Boolean type;
	private GoodsListAdapter adapter;
	private String jsonData;
	private List<Goods> goodList;
	private LinearLayout network_error;
	private LinearLayout noresult;

	public PersonalInfoFragment(String jsonData) {
		this.jsonData = jsonData;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initData();
		this.listView.setOnItemClickListener(this);
	}

	private void initData() {
		typeToken = new TypeToken<List<Goods>>() {
		}.getType();
		Log.i("tx", jsonData);
		if (TextUtils.isEmpty(jsonData) || jsonData.equals("[]")) {
			listView.setVisibility(View.GONE);
			noresult.setVisibility(View.VISIBLE);
		} else if (jsonData.equals("network_error")) {
			listView.setVisibility(View.GONE);
			network_error.setVisibility(View.VISIBLE);
		} else {
			goodList = JsonUtil.json2List(jsonData, typeToken);
			adapter = new GoodsListAdapter(goodList, getActivity());
			listView.setAdapter(adapter);
		}

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.user_detail_listview, null);
		this.listView = (ListView) rootView.findViewById(R.id.listview);
		this.network_error = (LinearLayout) rootView
				.findViewById(R.id.network_error);
		this.noresult = (LinearLayout) rootView.findViewById(R.id.noresult);
		return rootView;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Goods good = new Goods();
		good = goodList.get(position);
		Intent intent = new Intent(getActivity(), GoodsDetailActivity.class);
		intent.putExtra("price", good.getPrice());
		intent.putExtra("title", good.getGoods_name());
		intent.putExtra("place", good.getRequirements());
		intent.putExtra("detail", good.getDetail());
		intent.putExtra("uid", good.getUid());
		intent.putExtra("gid", good.getGid());
		intent.putExtra("isgoods", good.getIsgoods());
		intent.putExtra("publish_time", good.getDate());
		intent.putExtra("pathList", good.getGoods_imagepath());
		intent.putExtra("bookNumber", good.getBookingnumber());
		getActivity().startActivity(intent);
		getActivity().overridePendingTransition(R.anim.slide_in_right,
				R.anim.slide_out_left);
	}
}
